package com.iflytek.crvoice2txt.widget;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iflytek.crvoice2txt.R;
import com.iflytek.crvoice2txt.util.AudioUtil;

public class VoiceView extends LinearLayout {

    private LayoutInflater layoutInflater;

    private RelativeLayout rl_voice_view_bubblelong;

    private TextView tv_voice_view_timelong;

    private ImageView iv_voice_view;

    private Context context;

    public boolean isPlaying = false;

    private AnimationDrawable animationDrawable;

    private String AUDIO_PATH = null;

    private AudioUtil audioUtil;

    public void setAUDIO_PATH(String AUDIO_PATH) {
        this.AUDIO_PATH = AUDIO_PATH;
    }

    public VoiceView(Context context) {
        super(context);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.voice_view, this);
        initView();
    }

    public VoiceView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.voice_view, this);
        initView();
    }

    private void initView() {
        audioUtil = new AudioUtil();
        iv_voice_view = findViewById(R.id.iv_voice_view);
        iv_voice_view.setImageResource(R.drawable.sound3);
        rl_voice_view_bubblelong = findViewById(R.id.ll_voice_view_bubblelong);
        tv_voice_view_timelong = findViewById(R.id.tv_voice_view_timelong);
        rl_voice_view_bubblelong.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_voice_view.setImageResource(R.drawable.btn_play_animation);
                animationDrawable = (AnimationDrawable) (iv_voice_view).getDrawable();
                if (!isPlaying) {
                    animationDrawable.start();
                    audioUtil.startPlay(AUDIO_PATH);
                } else {
                    audioUtil.stopPlay();
                    animationDrawable.stop();
                    iv_voice_view.setImageResource(R.drawable.sound3);
                }
                isPlaying = !isPlaying;
            }
        });
        audioUtil.setAudioEvent(new AudioUtil.AudioEvent() {
            @Override
            public void onAudioPlayEnd(MediaPlayer mp) {
                animationDrawable.stop();
                iv_voice_view.setImageResource(R.drawable.sound3);
                isPlaying = false;
            }
        });
    }

    public void refreshVoiceView(int time) {

        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) rl_voice_view_bubblelong.getLayoutParams();
        lp.width = time;
        rl_voice_view_bubblelong.setLayoutParams(lp);
    }

    public void setTimeLong(int sed) {
        tv_voice_view_timelong.setText(sed + "'");
        refreshVoiceView(translateLenght(sed));
    }

    private int translateLenght(int sed) {
        int i = 190;
        /*if (sed <= 10 && sed > 0) {
            i = 300;
        } else if (sed <= 20 && sed > 10) {
            i = 350;
        } else if (sed <= 30 && sed > 20) {
            i = 400;
        } else if (sed <= 40 && sed > 30) {
            i = 450;
        } else if (sed <= 50 && sed > 40) {
            i = 500;
        } else if (sed <= 60 && sed > 50) {
            i = 550;
        }*/
        return i + 6 * sed;
    }

}